package com.pai4j.pgc.dao.community;
import com.pai4j.pgc.entity.community.ResourceDownloadEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface IResourceDownloadDAO extends JpaSpecificationExecutor<ResourceDownloadEntity>,
        JpaRepository<ResourceDownloadEntity, Long> {

    /***
     * 查询最近一段时间内的下载记录
     * @param startDate
     * @return
     */
    List<ResourceDownloadEntity> findAllByDownloadDateAfter(Date startDate);

    Long countAllByResourceId(String resourceId);

    @Query("select distinct (rde.userId) from ResourceDownloadEntity rde where rde.resourceId = ?1")
    List<String> findDistinctUserIdFromDownloadRecord(String resourceId);

    Page<ResourceDownloadEntity> findAllByUserId(String userId, Pageable pageable);
}
